package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.yxx.annotation.OptLogger;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.TaskQuery;
import com.yxx.entityModel.vo.request.StatusRequest;
import com.yxx.entityModel.vo.request.TaskRequest;
import com.yxx.entityModel.vo.request.TaskRunRequest;
import com.yxx.entityModel.vo.response.TaskBackResponse;
import com.yxx.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.yxx.constant.OptTypeConstant.*;

/**
 * 定时任务控制器
 * @author yuxiangxun
 * @date 2025/2/16
 * @apiNote
 */
@Api(tags = "定时任务模块")
@RestController
public class TaskController {
    //@Autowired
    //private TaskService taskService;

    // 尝试一下构造器注入
    private final TaskService taskService;

    @Autowired
    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    /**
     * 查看定时任务列表
     * @param taskQuery
     * @return
     */
    @ApiOperation("查看定时任务列表")
    @SaCheckPermission("monitor:task:list")
    @GetMapping("/admin/task/list")
    public ResponseResult<PageResult<TaskBackResponse>> taskBackList(TaskQuery taskQuery){
        // fix: 修复后台无法查看定时任务列表问题
        return ResponseResult.success(taskService.taskBackList(taskQuery));
    }

    /**
     * 添加定时任务
     * @param taskRequest 定时任务信息
     * @return
     */
    @OptLogger(value = ADD)
    @ApiOperation("添加定时任务")
    @SaCheckPermission("monitor:task:add")
    @PostMapping("/admin/task/add")
    public ResponseResult<?> addTask(@Validated @RequestBody TaskRequest taskRequest){
        taskService.addTask(taskRequest);
        return ResponseResult.success();
    }

    /**
     * 修改定时任务
     * @param taskRequest 定时任务信息
     * @return
     */
    @OptLogger(value = UPDATE)
    @ApiOperation("修改定时任务")
    @SaCheckPermission("monitor:task:update")
    @PutMapping("/admin/task/update")
    public ResponseResult<?> updateTask(@Validated @RequestBody TaskRequest taskRequest){
        taskService.updateTask(taskRequest);
        return ResponseResult.success();
    }

    /**
     * 删除定时任务
     * @param taskIdList 定时任务id集合
     * @return
     */
    @OptLogger(value = DELETE)
    @ApiOperation("删除定时任务")
    @SaCheckPermission("monitor:task:delete")
    @DeleteMapping("/admin/task/delete")
    public ResponseResult<?> deleteTask(@RequestBody List<Integer> taskIdList){
        taskService.deleteTask(taskIdList);
        return ResponseResult.success();
    }

    /**
     * 修改定时任务状态
     * @param statusRequest
     * @return
     */
    @OptLogger(value = UPDATE)
    @ApiOperation("修改定时任务状态")
    @SaCheckPermission(value = {"monitor:task:update","monitor:task:status"}, mode = SaMode.OR)
    @PutMapping("/admin/task/changeStatus")
    public ResponseResult<?> updateTaskStatus(@Validated @RequestBody StatusRequest statusRequest){
        taskService.updateTaskStatus(statusRequest);
        return ResponseResult.success();
    }

    /**
     * 执行定时任务
     * @param taskRunRequest 运行定时任务
     * @return
     */
    @OptLogger(value = UPDATE)
    @ApiOperation("执行定时任务")
    @SaCheckPermission("monitor:task:run")
    @PutMapping("/admin/task/run")
    public ResponseResult<?> runTask(@RequestBody TaskRunRequest taskRunRequest){
        taskService.runTask(taskRunRequest);
        return ResponseResult.success();
    }
}
